package com.hxkj.service.impl;

import com.hxkj.client.service.FunctionLibraryServiceClient;
import com.hxkj.domain.enums.FunctionLibraryExceptionEnum;
import com.hxkj.domain.po.FunctionLibrary;
import com.hxkj.domain.po.FunctionLibraryTree;
import com.hxkj.service.FunctionLibraryService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ResultMessageStruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName FunctionLibraryServiceImpl
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/27 12:41
 * @Version 1.0
 */
@Service
@Slf4j
public class FunctionLibraryServiceImpl implements FunctionLibraryService{
    @Autowired
    private FunctionLibraryServiceClient functionLibraryServiceClient;

    @Override
    public void saveFunctionLibrary(FunctionLibrary functionLibrary) throws Exception{
        if ( StringUtils.isEmpty(functionLibrary.getFunctionName())){
            throw new CustomException(FunctionLibraryExceptionEnum.FUNCTION_NAME_NOT_NULL);
        }
        if ( functionLibrary.getFunctionType() == null ){
            throw new CustomException(FunctionLibraryExceptionEnum.FUNCTION_TYPE_NOT_NULL);
        }
        if ( functionLibrary.getFunctionStatus() == null){
            throw new CustomException(FunctionLibraryExceptionEnum.FUNCTION_STATUS_NOT_NULL);
        }
        try{
            ResultMessageStruct resultMessageStruct= functionLibraryServiceClient.saveFunctionLibrary(functionLibrary);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(FunctionLibraryExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void modifyFunctionLibrary(FunctionLibrary functionLibrary) throws Exception{
        if (functionLibrary.getId() == null){
            throw new CustomException(FunctionLibraryExceptionEnum.FUNCTION_ID_NOT_NULL);
        }
        try{
            ResultMessageStruct resultMessageStruct= functionLibraryServiceClient.modifyFunctionLibrary(functionLibrary);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(FunctionLibraryExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<FunctionLibraryTree> getFunctionLibraryListTree(FunctionLibrary functionLibrary) throws Exception{
        List<FunctionLibraryTree> functionLibraryTreeList = new ArrayList<>();
        try{
            if ( StringUtils.isEmpty(functionLibrary.getKeyword())) {
                functionLibraryTreeList = functionLibraryServiceClient.getFunctionLibraryListTree(functionLibrary);
            } else {
                functionLibraryTreeList = functionLibraryServiceClient.getFunctionLibraryListTreeByKeyword(functionLibrary);
            }
            return functionLibraryTreeList;
        }catch(Exception e){
            e.printStackTrace();
            log.error("获取功能树失败:{}", e.getMessage());
            throw e;
        }
    }

    @Override
    public List<FunctionLibraryTree> getShowFunctionLibraryListTree(FunctionLibrary functionLibrary) throws Exception{
        if (functionLibrary.getSiteId() == null){
            throw new CustomException(FunctionLibraryExceptionEnum.SITE_ID_NOT_NULL);
        }
        try{
            // Mr.zheng:将站点ID放入站点ID字符串中然后根据他去查找
            functionLibrary.setSiteIds(String.valueOf(functionLibrary.getSiteId()));
            List<FunctionLibraryTree> functionLibraryTreeList= functionLibraryServiceClient.getShowFunctionLibraryListTree(functionLibrary);
            return functionLibraryTreeList;
        }catch(Exception e){
            e.printStackTrace();
            log.error("获取功能树失败:{}", e.getMessage());
            throw e;
        }
    }

    @Override
    public void removeFunctionLibrary(FunctionLibrary functionLibrary) throws Exception{
        if (functionLibrary.getSiteId() == null){
            throw new CustomException(FunctionLibraryExceptionEnum.SITE_ID_NOT_NULL);
        }
        try{
            ResultMessageStruct resultMessageStruct= functionLibraryServiceClient.removeFunctionLibrary(functionLibrary);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(FunctionLibraryExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void functionToSite(FunctionLibrary functionLibrary) throws Exception{
        ResultMessageStruct resultMessageStruct;
        if (StringUtils.isEmpty(functionLibrary.getIds())){
            throw new CustomException(FunctionLibraryExceptionEnum.FUNCTION_ID_NOT_NULL);
        }
        if (functionLibrary.getSiteId() == null){
            throw new CustomException(FunctionLibraryExceptionEnum.SITE_ID_NOT_NULL);
        }
        try{
            resultMessageStruct = functionLibraryServiceClient.functionToSite(functionLibrary);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomException(FunctionLibraryExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }
}
